import { _decorator, Component, Button, Node, Label, isValid } from 'cc';
import { Game } from '../core/Game';
import { Databus } from '../core/Databus';
import { BilliardGame } from './BilliardGame';
import { EventManager } from '../core/EventManager';
import { SoundManager } from '../framework/SoundManager';
import { updateSetting } from '../services/Setting';
import { GameDataAdaptor } from './GameDataAdaptor';
import DataEventReport from '../platform/DataEventReport';
import { InterstitialAd } from '../platform/ad/InterstitialAd';
const { ccclass, property } = _decorator;

const databus = Databus.Instance();
@ccclass('FastEndGame')
export class FastEndGame extends Component {
  private isLevelPass = false;
  private timestamp = 0;
  private changeLevelName: Node = null;
  private endLevelGame: Node = null;
  private passLevelGame: Node = null;
  private restartButton: Button = null;
  private backHomeButton: Button = null;
  onLoad(): void {
    this.changeLevelName = this.node.getChildByName('ChangeLevelName');
    this.endLevelGame = this.node.getChildByName('EndLevelGame');
    this.passLevelGame = this.node.getChildByName('PassLevelGame');
    this.restartButton = this.node.getChildByName('EndLevelGame').getChildByName('RestartPlay').getComponent(Button);
    this.backHomeButton = this.node.getChildByName('EndLevelGame').getChildByName('BackHome').getComponent(Button);
    EventManager.Instance().AddEventListener('FastStartFancyGame', this, this.fastStartFancyGame);
    EventManager.Instance().AddEventListener('FastEndFancyGame', this, this.fastEndFancyGame);
    this.restartButton.node.on(Button.EventType.CLICK, this.restartGame, this);
    this.backHomeButton.node.on(Button.EventType.CLICK, this.backToHomePage, this);

    this.node.active = false;
    this.changeLevelName.active = false;
    this.endLevelGame.active = false;
    this.passLevelGame.active = false;
  }

  update(deltaTime: number) {
    this.timestamp += 1;
    if (!this.isLevelPass) {
      return;
    }
    if (this.timestamp < 60) {
      this.changeLevelName.active = false;
      this.passLevelGame.active = true;
    } else {
      this.changeLevelName.active = true;
      this.passLevelGame.active = false;
    }
  }

  public restartGame(button: Button) {
    if (Game.Instance().battleCate.isFancyBattle()) {
      this.node.getParent().active = false;
      this.endLevelGame.active = false;
      this.changeLevelName.active = false;
      this.passLevelGame.active = false;
      this.node.active = false;
      EventManager.Instance().Emit('RestartGame', {});
      return;
    }
  }

  public backToHomePage(button: Button) {
    this.node.getParent().active = false;
    this.node.active = false;
    this.endLevelGame.active = false;
    this.changeLevelName.active = false;
    BilliardGame.Instance().leaveRoomPage();
    databus.sceneStack.backHomePage();
    InterstitialAd.Instance().show();
  }

  public fastStartFancyGame(name: string, data: any) {
    this.timestamp = 0;
    this.isLevelPass = true;
    this.updateUserGoldCoin(databus.hurdles.getHurdlesConfig().coin);
    databus.hurdles.addLevel();
    this.updateBarrierLevel();
    console.log('fast start fancy game');
    this.node.getParent().active = true;
    this.node.active = true;
    this.endLevelGame.active = false;
    this.changeLevelName.active = false;
    this.passLevelGame.active = true;
    const label = this.changeLevelName.getChildByName('LevelName').getComponent(Label);
    label.string = `${databus.hurdles.levelIndex}`;
    SoundManager.Instance().playGameWin(1.0);
    const that = this;
    setTimeout(() => {
      if (isValid(that.node)) {
        that.node.getParent().active = false;
        that.node.active = false;
        that.changeLevelName.active = false;
        that.passLevelGame.active = false;
        EventManager.Instance().Emit('RestartGame', {});
      }
    }, 5000);
    DataEventReport.Instance().reportCoreMetric(
      'fancy_win', Game.Instance().battleCate.valueBattle(),
      Game.Instance().gameType.valueType(),
    );
    InterstitialAd.Instance().show();
  }

  public fastEndFancyGame(name: string, data: any) {
    console.log('fast end fancy game ');
    this.isLevelPass = false;
    this.node.getParent().active = true;
    this.node.active = true;
    this.endLevelGame.active = true;
    this.changeLevelName.active = false;
    this.passLevelGame.active = false;
    SoundManager.Instance().playGameLose(0.8);

    DataEventReport.Instance().reportCoreMetric(
      'fancy_lose', Game.Instance().battleCate.valueBattle(),
      Game.Instance().gameType.valueType(),
    );
    InterstitialAd.Instance().show();
  }
  private updateBarrierLevel() {
    const lv = databus.hurdles.levelIndex;
    updateSetting(databus.selfInfo.userId, databus.selfInfo.userId, 2, 'currentBarrierLevel', `${lv}`);
  }

  private updateUserGoldCoin(gc: number) {
    if (gc > 0) {
      GameDataAdaptor.Instance().updateUserGoldCoin(gc);
    }
  }
}